﻿<?php
	class WebApp
	{
		static private $_application = NULL;
		static private $_app = NULL;
		public $registry;
		protected $router;
		protected $config;
		
		public static function __autoloadMVC($class_name)
		{
			$filename = strtolower($class_name) . '.php';
			if($class_name == "")
			$filename = strtolower($class_name) . '.php';
			$file = site_path . 'classes' . DIRSEP . $filename;
	
			if (file_exists($file) == false) {
	
					return false;
			}
	
			include ($file);
		}
		public function __get($name)
		{
			return isset($this->registry[$name]) ? $this->registry[$name] : NULL;
		}
		private function __construct($cfg)
		{
			WebApp::setApp($this);
			$this->config = require($cfg);
			spl_autoload_register("WebApp::__autoloadMVC");
			$this->registry = new Registry;

			$this->registry->set('config', $cfg);
			$this->build();
			
			$this->router = new Router($this->registry);
			
			$this->registry->set ('router', $this->router);
			
			$this->router->setPath (site_path . 'controllers');
			
			$dbConnect = $this->config['dbConnect'];
			
			$this->registry->set('db', new DB($dbConnect['host'],$dbConnect['dbname'],$dbConnect['user'],$dbConnect['password']));
			
			$this->registry->set('baseController', isset($this->config['baseController']) ? $this->config['baseController'] : 'index');
		}
		
		public static function app()
		{
			return self::$_app;
		}
		public static function setApp($app)
		{
			self::$_app = $app;
		}
		
		public static function createWebApp($cfg)
		{
			if (self::$_application == NULL)
			{
			  	self::$_application = new WebApp($cfg);
			}
			return self::$_application;
		}
		
		public function run()
		{
			$this->router->delegate();
		}
		
		protected function build()
		{
			$this->registry->set('theme', $this->config['theme']);
		}
		
		public function getThemePath()
		{
			return $this->getBaseUrl().'themes'.'/'.$this->registry->get('theme').'/';
		}
		public function getBaseUrl()
		{
			$count_sim = strrpos($_SERVER['SCRIPT_NAME'], '/');
			$str = substr($_SERVER['SCRIPT_NAME'], 0, $count_sim + 1);
			$newstr = 'http://'.$_SERVER['HTTP_HOST'].$str;
			return $newstr;
		}
		function image_resize($sourse, $new_image, $width, $height)
		{
			$size = getimagesize($sourse);
			$new_height = $height;
			$new_width = $width;
		
			if ($size[0] < $size[1])
			$new_width=($size[0]/$size[1])*$height;
			else
			{
				$new_height=($size[1]/$size[0])*$width;
				$new_width=($new_width > $width)?$width:$new_width;
				$new_height=($new_height > $height)?$height:$new_height;
			}
			$image_p = @imagecreatetruecolor($new_width, $new_height);
			if ($size[2]==2)
			{
				$image_cr = imagecreatefromjpeg($sourse);
			}
			  else if ($size[2]==3)
			  {
			   $image_cr = imagecreatefrompng($sourse);
			  }
			  else if ($size[2]==1)
			  {
			   $image_cr = imagecreatefromgif($sourse);
			  }
			  imagecopyresampled($image_p, $image_cr, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
			  if ($size[2]==2)
			  {
			   imagejpeg($image_p, $new_image, 75);
			  }
			  else if ($size[2]==1)
			  {
			   imagegif($image_p, $new_image);
			  }
			  else if ($size[2]==3)
			  {
			  imagepng($image_p, $new_image);
			  }
		}
	}